clear all

set more off
set matsize 10000

global base "X:\Dropbox\educacion superior 2021\submission\EDCC\For publication\Replication package\Data"
global output "X:\Dropbox\educacion superior 2021\submission\EDCC\For publication\Replication package\Output"

cd "$base"
use "output/panel_final"

*** Table 1

qui putexcel set "$output/Table-Balance.xlsx", replace

cap mat drop Means
mat Means = J(32,4,.)
local r = 1

foreach var in skills_cr_r3 skills_nc2_r3 terwi1_r3 terwi2_r3 terwi3_r3 ///
	careed_prim careed_sec careed_ter rural_r3 zhfa_r3 migrated_r1 ///
	indigena_r1 order_1 msib_r3 fsib_r3 ///
	panel1234 { 
		
	qui mean `var' /*if panel1234 == 1*/, vce(cl clustid)

	mat Means[`=(`r'-1)*2+1', 1] = `=round(r(table)[1,1],0.01)'
	mat Means[`=(`r'-1)*2+2', 1] = `=round(r(table)[2,1],0.01)'
			
	qui mean `var' /*if panel1234 == 1*/, over(sex) vce(cl clustid)
	
	mat Means[`=(`r'-1)*2+1', 2] = `=round(r(table)[1,1],0.01)'
	mat Means[`=(`r'-1)*2+2', 2] = `=round(r(table)[2,1],0.01)'
	mat Means[`=(`r'-1)*2+1', 3] = `=round(r(table)[1,2],0.01)'
	mat Means[`=(`r'-1)*2+2', 3] = `=round(r(table)[2,2],0.01)'
	
	qui reg `var' sex, vce(cl clustid)
	qui lincom sex
	
	mat Means[`=(`r'-1)*2+1', 4] = `=round(`r(p)',0.001)'
		
	qui putexcel B`=2+(`r'-1)*2+1' = "`var'"
		
	local ++r
	
}

qui putexcel C3 = matrix(Means), hcenter
qui putexcel 	C2 = "Full" 	///
				D2 = "Male" 	///
				E2 = "Female"	///
				F2 = "M=F pval"	///
				, hcenter
				
*** Table 2

qui putexcel set "$output/Table-SummaryStats.xlsx", replace

qui putexcel 	C2 = "Full" 	///
				D2 = "Male" 	///
				E2 = "Female" 	///
				F2 = "M=F pval"	///
				, hcenter
		
local v = 1
foreach var in finish_school enroll_sup2 still_sup2 { // using enroll_sup2 and still_sup2 because children still in school are coded as zero here

	qui putexcel B`=3+(`v'-1)*5' = "`var'"
	cap mat drop Means_`var'

	local r = 1
	mat Means_`var' = J(4,4,.)
		
	forvalues ter = 1/3 {

		qui putexcel B`=3+(`v'-1)*5+`ter'' = "wi ter = `ter'"

		qui mean `var' if panel1234 == 1 & terwi`ter'_r3 == 1

		mat Means_`var'[`r', 1] = `=round(r(table)[1,1]*100,0.1)'
				
		qui mean `var' if panel1234 == 1 & terwi`ter'_r3 == 1, over(sex)
		
		mat Means_`var'[`r', 2] = `=round(e(b)[1,1]*100,0.1)'
		mat Means_`var'[`r', 3] = `=round(e(b)[1,2]*100,0.1)'
		
		qui reg `var' sex if panel1234 == 1 & terwi`ter'_r3 == 1, vce(cl clustid)
		qui lincom sex
		
		mat Means_`var'[`r', 4] = `=round(`r(p)',0.001)'
			
		local ++r
		
	}

	qui putexcel B`=3+(`v'-1)*5+4' = "total"

	qui mean `var' if panel1234 == 1

	mat Means_`var'[`r', 1] = `=round(r(table)[1,1]*100,0.1)'
			
	qui mean `var' if panel1234 == 1, over(sex)
	
	mat Means_`var'[`r', 2] = `=round(e(b)[1,1]*100,0.1)'
	mat Means_`var'[`r', 3] = `=round(e(b)[1,2]*100,0.1)'
	
	qui reg `var' sex if panel1234 == 1, vce(cl clustid)
	qui lincom sex
	
	mat Means_`var'[`r', 4] = `=round(`r(p)',0.001)'

	* Outputting
	
	qui putexcel C`=4+(`v'-1)*5' =  matrix(Means_`var'), hcenter

	local ++v

}

*===============================================================================